﻿
Module MODdbMethods

    Dim _
    openStr As String, _
    intTotalMatches As Integer, _
    strFieldsList As String

    Public strColumnWidths As String

    Private _
        oConn As ADODB.Connection, _
        oRS As ADODB.Recordset, _
        oField As ADODB.Field

    Private sDatabasePath As String

    Private strRowToUpdateList As String

    Public fso As Scripting.FileSystemObject

    Public Function init()

        On Error Resume Next

        Dim sFilePath As String

        sFilePath = getPref("", "defaultDatabasePath")

        fso = New Scripting.FileSystemObject

        If Not fso.FileExists(sFilePath) Then Exit Function

        ' Open the default database ---------------------------------------------------------------------------------------------

        openConnection(sFilePath)

    End Function

    Public Sub openConnection(ByVal sDatabasePath As String)
        Throw New NotImplementedException
        'On Error Resume Next

        'Dim sConn As String

        'oConn = New ADODB.Connection

        'sConn = "PROVIDER=MICROSOFT.JET.OLEDB.4.0;DATA SOURCE=" & sDatabasePath

        'FRMmain.Adodc1.ConnectionString = sConn

        'oConn.Open(FRMmain.Adodc1.ConnectionString)

        'If Err.Number <> 0 Then

        '    logMessage( _
        '        1, _
        '        "MODdbMethods", _
        '        "Could not open ADODB connection", _
        '        "Connection String = " & sConn, _
        '        Err.Number, _
        '        Err.Description)

        '    MsgBox( _
        '        "Could not open connection to database : " & sDatabasePath & vbCrLf & _
        '            "Reason : " & Err.Description)

        '    Exit Sub

        'Else

        'End If

        'oRS = oConn.OpenSchema(adSchemaTables)

        'FRMmain.LISTtables.Items.Clear()

        'Do Until oRS.EOF

        '    If oRS!TABLE_TYPE = "TABLE" Then

        '        FRMmain.LISTtables.Items.Add(oRS!TABLE_NAME)

        '    End If

        '    oRS.MoveNext()

        'Loop

        'FRMmain.ListFields.Text = "All"
        'FRMmain.LISTtables.SelectedIndex = 0

        'oRS.Close()
        'oConn.Close()

        'oRS = Nothing
        'oConn = Nothing

    End Sub

    ' Loop through the fields in the selected table, and add each one to the listbox in the main form -------------------------------------------------------------------------

    Public Function setFieldsList(ByVal sTable As String, ByVal sDefaultField As String)
        Throw New NotImplementedException
        '        On Error Resume Next

        '        fso = New Scripting.FileSystemObject

        '        Dim _
        '            sRSOpen As String

        '        Dim i As Integer

        '        oConn = New ADODB.Connection
        '        oRS = New ADODB.Recordset

        '        oConn.Open(FRMmain.Adodc1.ConnectionString)

        '        If Err.Number <> 0 Then

        '            Exit Function

        '        End If

        '        sRSOpen = "SELECT * FROM " & sTable

        '        oRS.Open(sRSOpen, oConn, 1, 3)

        '        FRMmain.ListFields.Items.Clear()

        '        FRMmain.ListFields.Items.Add("All")

        '        ' Loop through the fields in the table, and add them to the list box ---------------------------------------------------------------------------------

        '        For Each oField In oRS.Fields

        '            FRMmain.ListFields.Items.Add(oField.Name)

        '        Next

        '        ' Select the default search field --------------------------------------------------------------------------------------------------

        '        For i = 0 To FRMmain.ListFields.Items.Count

        '            If FRMmain.ListFields.Items(i) = sDefaultField Then

        '                FRMmain.ListFields.SetSelected(i, True)

        '                Exit For

        '            End If

        '        Next

        '        oRS.Close()
        '        oConn.Close()


        '        oRS = Nothing
        '        oConn = Nothing

        'errHandler:

        '        If Err.Number = -2147467259 Then

        '            MsgBox("The dataBase you are attempting to open is already open outside of the application")

        '        End If

        '        Exit Function

    End Function
    '---------------------------------------'
    '   Search DataBase                     '
    '---------------------------------------'
    Public Function searchDB(ByVal tableToSearch As String, ByVal fieldTosearch As String, ByVal queryStr As String)
        Throw New NotImplementedException
        On Error Resume Next

        '        ''on error GoTo handleError
        '        FRMmain.MousePointer = 11
        '        strWhere = ""

        '        '---------------------------------------------------------------------------------------'
        '        '   get string matching criteria from radio buttons                                     '
        '        '---------------------------------------------------------------------------------------'
        '        If FRMmain.optionAnyMatch.Value = True Then
        '            queryStr = "%" & queryStr & "%"
        '        ElseIf FRMmain.optionExactMatch.Value = True Then
        '            queryStr = queryStr
        '        ElseIf FRMmain.optionStartsWith.Value = True Then
        '            queryStr = queryStr & "%"
        '        ElseIf FRMmain.optionEndsWith.Value = True Then
        '            queryStr = "%" & queryStr
        '        ElseIf FRMmain.optionWildCard.Value = True Then
        '            queryStr = Replace(queryStr, " ", "")
        '            queryStr = Replace(queryStr, "/", "")
        '            queryStr = Replace(queryStr, "\", "")
        '            queryStr = Replace(queryStr, "-", "")
        '            queryStr = Replace(queryStr, "/", "")
        '            queryStr = Replace(queryStr, "(", "")
        '            queryStr = Replace(queryStr, ")", "")
        '            queryStr = Replace(queryStr, "[", "")
        '            queryStr = Replace(queryStr, "]", "")
        '            queryStr = Replace(queryStr, "_", "")
        '            queryStr = Replace(queryStr, "|", "")
        '            queryStr = Replace(queryStr, """", "")
        '            queryStr = Replace(queryStr, "'", "")
        '            queryStr = Replace(queryStr, ",", "")
        '            queryStr = Replace(queryStr, ".", "")
        '            tempstr = "%"
        '            For i = 1 To Len(queryStr)
        '                tempstr = tempstr & Mid(queryStr, i, 1) & "%"
        '            Next
        '            queryStr = tempstr
        '        End If
        '        '---------------------------------------------------------------------------------------'
        '        '   create "WHERE" clause for SQL statement                                             '
        '        '---------------------------------------------------------------------------------------'
        '        If fieldTosearch = "All" Then
        '            strWhere = " WHERE "
        '            arrFieldsList = Split(strFieldsList, ",")
        '            For Each fldName In arrFieldsList
        '                strWhere = strWhere & tableToSearch & ".[" & fldName & "] LIKE '" & queryStr & "' OR "
        '            Next
        '            strWhere = Utils.Left(strWhere, Len(strWhere) - 3)
        '        Else
        '            strWhere = " WHERE " & tableToSearch & ".[" & fieldTosearch & "] LIKE '" & queryStr & "'"
        '        End If
        '        openStr = "SELECT * FROM " & tableToSearch & strWhere
        '        FRMmain.Adodc1.RecordSource = openStr
        '        FRMmain.Adodc1.Refresh()
        '        Dim rs As Recordset
        '        rs = FRMmain.Adodc1.Recordset
        '        FRMmain.LBLresultsCount.Caption = rs.RecordCount
        '        '---------------------------------------------------------------------------------------'
        '        '   set column widths for the dataGrid after each search                                '
        '        '---------------------------------------------------------------------------------------'
        '        i = 0
        '        For Each fld In rs.Fields

        '            If IsArray(aColumnWidths) Then

        '                If IsNumeric(aColumnWidths(i)) Then

        '                    intColumnWidth = aColumnWidths(i)

        '                Else

        '                    intColumnWidth = 50

        '                End If

        '            Else

        '                intColumnWidth = 50

        '            End If

        '            FRMmain.DataGrid.Columns(i).Width = intColumnWidth

        '            If IsArray(aColumnWidths) Then

        '                If i < UBound(aColumnWidths) Then

        '                    i = i + 1

        '                End If

        '            End If

        '        Next
        '        intTotalSearchResults = 0
        '        FRMmain.MousePointer = 0
        '        strWhere = ""

        'handleError:
        '        If Err.Number <> 0 Then
        '            If Err.Number = -2147217900 Then
        '                MsgBox("You Have Entered Invalid Search Criteria" & vbCrLf & "Please Try Again")
        '                FRMmain.TXTSearch.Focus()
        '                FRMmain.TXTsearch.SelStart = 0
        '                FRMmain.TXTsearch.SelLength = Len(FRMmain.TXTsearch.Text)
        '            Else
        '                MsgBox("Error : " & Err.Description & "Error Number : " & Err.Number)
        '            End If
        '        End If
        '        FRMmain.MousePointer = 0
    End Function
    '-----------------------------------------------------'
    '   Save edited row to database                       '
    '-----------------------------------------------------'
    Public Function setRowToUpdate(ByVal strFieldsList)

        On Error Resume Next

        strRowToUpdateList = strFieldsList
    End Function
    Public Function saveRow()
        Throw New NotImplementedException
        'On Error Resume Next

        'Dim _
        '    conn As ADODB.Connection, _
        '    rs As ADODB.Recordset

        'Dim _
        '    strConn As String, _
        '    openStr As String

        'conn = New ADODB.Connection
        'rs = New ADODB.Recordset

        'strConn = "PROVIDER=MICROSOFT.JET.OLEDB.4.0;DATA SOURCE=" & sDatabasePath

        'conn.Open(strConn)

        'rs.ActiveConnection = conn

        'openStr = "UPDATE " & FRMmain.LISTtables.Items(FRMmain.LISTtables.SelectedIndex) & "" & _
        '    " SET [" & FRMeditRow.fieldName(1).Caption & "] = '" & FRMeditRow.Field(1).Text & "'"

        'For i = 2 To FRMeditRow.Field.Count - 1 - 5

        '    openStr = openStr & _
        '        " , " & _
        '        "[" & _
        '        FRMeditRow.fieldName(i).Caption & _
        '        "] = '" & _
        '        FRMeditRow.Field(i).Text & "'"

        'Next

        'splitStrRowToUpdateList = Split(strRowToUpdateList, "||")

        'openStr = openStr & " WHERE [" & FRMeditRow.fieldName(1).Caption & "] = '" & _
        '    splitStrRowToUpdateList(0) & "'"

        'For i = 2 To UBound(splitStrRowToUpdateList) - 5
        '    openStr = openStr & " AND [" & FRMeditRow.fieldName(i).Caption & "] = '" & _
        '        splitStrRowToUpdateList(i - 1) & "'"
        'Next
        ''openStr = openStr & ";"
        ''debug.print openStr
        'X = 1
        'rs.Open(openStr, conn, 1, 3)
        'FRMmain.Adodc1.Refresh()
        'conn.Close()
        'rs = Nothing
        'conn = Nothing
        'Unload(FRMeditRow)
    End Function
    '-------------------------------------------------------'
    '   Set the listBox for the import dialog               '
    '-------------------------------------------------------'
    Public Function setImportTablesCombo(ByVal importFileName As String)
        Throw New NotImplementedException
        'On Error Resume Next

        'Dim conn As ADODB.Connection
        'Dim rSchema As ADODB.Recordset
        'conn = New ADODB.Connection
        'conn.Open("Driver={Microsoft Excel Driver (*.xls)};DriverId=790;Dbq=" & importFileName)
        'rSchema = conn.OpenSchema(adSchemaTables)

        'DLGimport.COMBimportFileTable.Items.Clear()
        'Do Until rSchema.EOF
        '    If rSchema!TABLE_TYPE = "TABLE" Then
        '        DLGimport.COMBimportFileTable.Items.Add(rSchema!TABLE_NAME)
        '    End If
        '    rSchema.MoveNext()
        'Loop
        'If DLGimport.COMBimportFileTable.Items.Count > 0 Then
        '    DLGimport.COMBimportFileTable.SelectedIndex = 0
        'End If
        'rSchema.Close()
        'conn.Close()
        'rSchema = Nothing
        'conn = Nothing

    End Function

End Module
